/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import modelo.Indice;

/**
 *
 * @author Andrés
 */
public class VistaDesfragmentar extends javax.swing.JFrame {


    private final DefaultTableModel defaultTableModel;

    /**
     * Creates new form VistaInicio
     */
    public VistaDesfragmentar() {
        initComponents();
        defaultTableModel = (DefaultTableModel) tablaIndices.getModel();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        setVisible(false);
        setLocationRelativeTo(null);
        iniciarLogo();
    }

    public final void iniciarLogo() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("./image/logo.png")));
    }

    public void agregarListenerBotonAceptar(ActionListener listener) {
        this.botonAceptar.addActionListener(listener);
    }

    public void agregarListenerItemCerrar(ActionListener actionListener) {
        this.itemCerrar.addActionListener(actionListener);
    }

    public void agregarListenerItemAcerdaDe(ActionListener actionListener) {
        this.itemAcercaDe.addActionListener(actionListener);
    }

    public void agregarListenerBotonCancelar(ActionListener actionListener) {
        this.botonCancelar.addActionListener(actionListener);
    }

    public void agregarInfoIndices(ArrayList<Indice> indices) {
        if (defaultTableModel.getRowCount() > 0) {
            for (int i = defaultTableModel.getRowCount() - 1; i > -1; i--) {
                defaultTableModel.removeRow(i);
            }
        }
        for (Indice ind : indices) {
            defaultTableModel.addRow(new Object[]{false, ind.getNombre(), ind.getOwner(), ind.getFragmentacion()});
        }
        tablaIndices.repaint();

        this.repaint();
    }

    public ArrayList<Indice> obtenerDesfragmentables() {
        ArrayList<Indice> indicesD = new ArrayList<>();
        for (int i = defaultTableModel.getRowCount() - 1; i > -1; i--) {
            if ((boolean) defaultTableModel.getValueAt(i, 0)) {
                indicesD.add(new Indice((String) defaultTableModel.getValueAt(i, 1),
                        (String) defaultTableModel.getValueAt(i, 2),
                        (Float) defaultTableModel.getValueAt(i, 3)));
            }

        }
        return indicesD;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaIndices = new javax.swing.JTable();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        menu = new javax.swing.JMenuBar();
        menuArchivo = new javax.swing.JMenu();
        itemCerrar = new javax.swing.JMenuItem();
        menuAyuda = new javax.swing.JMenu();
        itemAcercaDe = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Monitor Administración Bases de Datos");

        tablaIndices.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Desfragmentar", "Nombre", "Owner", "% Fragmentacion"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaIndices);

        botonAceptar.setText("Aceptar");

        botonCancelar.setText("Cancelar");

        menuArchivo.setText("Archivo");

        itemCerrar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        itemCerrar.setText("Cerrar");
        menuArchivo.add(itemCerrar);

        menu.add(menuArchivo);

        menuAyuda.setText("Ayuda");

        itemAcercaDe.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        itemAcercaDe.setText("Acerca de");
        menuAyuda.add(itemAcercaDe);

        menu.add(menuAyuda);

        setJMenuBar(menu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botonCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonAceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VistaDesfragmentar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VistaDesfragmentar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VistaDesfragmentar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VistaDesfragmentar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaDesfragmentar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JMenuItem itemAcercaDe;
    private javax.swing.JMenuItem itemCerrar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuBar menu;
    private javax.swing.JMenu menuArchivo;
    private javax.swing.JMenu menuAyuda;
    private javax.swing.JTable tablaIndices;
    // End of variables declaration//GEN-END:variables
}
